package fnic.river;

import org.apache.log4j.Logger;

import fnic.prehand.common.AgentConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;

public abstract class RedisRiver {
	private static final AgentConfig config = AgentConfig.getInstance();
	protected static final Logger logger = Logger.getLogger(RedisRiver.class);
	private static final JedisPoolConfig redisconfig;
	
	//redis配置
	private final int redisDB = 0;
	protected static final int redisbulkTimeout = config.getRedistimeout();
	protected static final int redisoncesize =  config.getRedisoncedealnum();
	private final JedisPool jedisPool;
	protected final Jedis jedis;
	protected Pipeline jpline;
	protected String redisKey;
	
	protected long cycle = 0;
	
//	private List<String> response = null;
//	private Response<String>[] rsps =  new Response[redisoncesize];
	static{
  
        int maxActivity = config.getJedisMaxTotal();  
        int maxIdle = config.getJedisMaxIdle();  
        long maxWait = config.getJedismaxWaitMillis();  
        boolean testOnBorrow = config.isJedistestOnBorrow();  
        boolean onreturn = config.isJedistestOnReturn();  
	        
        //设置池配置项值  
        redisconfig = new JedisPoolConfig(); 
        redisconfig.setMaxTotal(maxActivity);
        redisconfig.setMaxIdle(maxIdle);    
        redisconfig.setMaxWaitMillis(maxWait); //milli seconds
        redisconfig.setTestOnBorrow(testOnBorrow);    
        redisconfig.setTestOnReturn(onreturn);  
	}
	
	public RedisRiver(String redisIp, int redisPort, String key){
		jedisPool = new JedisPool(redisconfig, redisIp, redisPort, config.getJedismaxWaitMillis());
		try{
			jedis = jedisPool.getResource();
		}catch(Exception e){
			logger.error("get redis resource fail:"+e.getMessage());
			throw new IllegalArgumentException("can't get a redis conn from redis pool! ip:"+redisIp+", port:"+redisPort);
		}
		jpline = jedis.pipelined();
		redisKey = key;
	}
	
	
/*	public void loopSingle() {
		int counter = 0;
		do {
			response = null;
			try {
				response = jedis.blpop(redisbulkTimeout, redisKey);
			} catch (Exception e) {
				// Can't get a redis object. Return and try again on th next loop.
				logger.info("Can't read from redis. Waiting 5 seconds and trying again.");
			}

			if (response != null) {
				String jsonsrc = response.get(1);
				dealRecord(jsonsrc);
			}
		} while (++counter < redisoncesize && response != null);
		cycle++;
	}*/
	
	
/*	public int loopBulk(){
		for (int i = 0; i < redisoncesize; i++) {
			rsps[i] = (Response<String>) jpline.rpop(redisKey);
		}
		try {
			jpline.sync();
		} catch (Exception e) {
			logger.error("!!!!------------redis sync get rsp fail: "+ e.getMessage());
			return -1;
		}
		for (int i = 0; i < redisoncesize; i++) {
			if (rsps[i] == null || rsps[i].get() == null) {
				return i;
			}
			dealRecord(rsps[i].get());
		}
		cycle++;
		return redisoncesize;
	}*/
	
	public void setRedisKey(String key){
		redisKey = key;
	}
	
//	public abstract void dealRecord(String obj);
}
